package com.zhangxu.microservice.system.biz.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhangxu.microservice.common.utils.AesEncryptUtils;
import com.zhangxu.microservice.common.utils.AssertUtils;
import com.zhangxu.microservice.system.biz.convert.EmployeeBizConverter;
import com.zhangxu.microservice.system.biz.dto.CreateEmployeeDTO;
import com.zhangxu.microservice.system.biz.dto.EmployeeDTO;
import com.zhangxu.microservice.system.biz.service.EmployeeBizService;
import com.zhangxu.microservice.system.dao.condition.EmployeeCondition;
import com.zhangxu.microservice.system.dao.entity.EmployeeDO;
import com.zhangxu.microservice.system.dao.enums.EmployeeStatusEnum;
import com.zhangxu.microservice.system.dao.service.EmployeeDao;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class EmployeeBizServiceImpl implements EmployeeBizService {

    private final EmployeeDao employeeDao;
    private final PasswordEncoder passwordEncoder;
    private final EmployeeBizConverter employeeBizConverter;

    @Override
    public Long createEmployee(CreateEmployeeDTO createEmployeeDTO) {
        EmployeeDO employeeDO = employeeDao.getByAccount(createEmployeeDTO.getAccount());
        AssertUtils.isNull(employeeDO, "账号已存在");
        //加密密码
        String initPwd = "123456";
        String passwordCrypto = passwordEncoder.encode(initPwd);
        createEmployeeDTO.setPasswordCrypto(passwordCrypto);
        String phoneCrypto = AesEncryptUtils.encrypt(createEmployeeDTO.getPhone());
        createEmployeeDTO.setPhoneCrypto(phoneCrypto);
        EmployeeDO crateEmployeeDO = employeeBizConverter.toEmployeeDO(createEmployeeDTO);
        employeeDao.save(crateEmployeeDO);
        return crateEmployeeDO.getId();
    }

    @Override
    public void enable(Long employeeId) {
        EmployeeDTO employeeDTO = getById(employeeId);
        AssertUtils.notNull(employeeDTO, "数据不存在");
        //判断只能新建状态可以启用
        AssertUtils.isFalse(EmployeeStatusEnum.ENABLE.name().equals(employeeDTO.getStatus()), "当前状态不能启用");
        EmployeeDO employeeDO = new EmployeeDO();
        employeeDO.setId(employeeId);
        employeeDO.setStatus(EmployeeStatusEnum.ENABLE.name());
        employeeDao.updateById(employeeDO);
    }

    @Override
    public void disable(Long employeeId) {
        EmployeeDTO employeeDTO = getById(employeeId);
        AssertUtils.notNull(employeeDTO, "数据不存在");
        //判断只能新建状态可以启用
        AssertUtils.isTrue(EmployeeStatusEnum.ENABLE.name().equals(employeeDTO.getStatus()), "当前状态不能禁用");
        EmployeeDO employeeDO = new EmployeeDO();
        employeeDO.setId(employeeId);
        employeeDO.setStatus(EmployeeStatusEnum.DISABLE.name());
        employeeDao.updateById(employeeDO);
    }

    @Override
    public void delete(Long employeeId) {
        EmployeeDTO employeeDTO = getById(employeeId);
        AssertUtils.notNull(employeeDTO, "数据不存在");
        //判断只能新建状态可以启用
        AssertUtils.isTrue(EmployeeStatusEnum.INIT.name().equals(employeeDTO.getStatus()), "当前状态不能删除");
        EmployeeDO employeeDO = new EmployeeDO();
        employeeDO.setId(employeeId);
        employeeDO.setDeleteTime(System.currentTimeMillis());
        employeeDao.updateById(employeeDO);
    }

    @Override
    public EmployeeDTO getByAccount(String account) {
        return employeeBizConverter.toEmployeeDTO(employeeDao.getByAccount(account));
    }

    @Override
    public EmployeeDTO getById(Long id) {
        return employeeBizConverter.toEmployeeDTO(employeeDao.getById(id));
    }

    @Override
    public List<EmployeeDTO> listByIds(List<Long> employeeIds) {
        return employeeDao.listByIds(employeeIds).stream()
                .map(employeeBizConverter::toEmployeeDTO)
                .collect(Collectors.toList());
    }

    @Override
    public IPage<EmployeeDTO> employeePage(EmployeeCondition condition) {
        return employeeDao.page(condition).convert(employeeBizConverter::toEmployeeDTO);
    }
}
